{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "4b90a557",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1084429e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: openai in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (1.3.6)\n",
      "Requirement already satisfied: anyio<4,>=3.5.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (3.5.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (1.8.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (0.24.1)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (1.10.12)\n",
      "Requirement already satisfied: sniffio in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (1.2.0)\n",
      "Requirement already satisfied: tqdm>4 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (4.65.0)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.5 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (4.7.1)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from anyio<4,>=3.5.0->openai) (3.4)\n",
      "Requirement already satisfied: certifi in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)\n",
      "Requirement already satisfied: httpcore<0.18.0,>=0.15.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (0.17.3)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from httpcore<0.18.0,>=0.15.0->httpx<1,>=0.23.0->openai) (0.14.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install openai==1.3.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "23dae813",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_current_cluster_state(cluster_name):\n",
    "    print(f\"cluster:{cluster_name}\")\n",
    "    return  \"\"\"ERROR: Failed to pull image \"chaocai/docker/dsp:latest\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "5c49d07f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import AzureOpenAI\n",
    "    \n",
    "client = AzureOpenAI(\n",
    "    api_key=os.getenv(\"OPENAI_API_KEY\"),  \n",
    "    api_version=os.getenv(\"OPENAI_API_VERSION\"),\n",
    "    azure_endpoint = os.getenv(\"OPENAI_API_BASE\")\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "d9469fb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import json\n",
    "\n",
    "\n",
    "funcs = {\"get_current_cluster_state\": get_current_cluster_state}\n",
    "\n",
    "\n",
    "def run(input):\n",
    "    msg=[{\"role\":\"user\",\"content\":input}]\n",
    "    ret = run_conversation(msg)\n",
    "    return ret.content\n",
    "\n",
    "def run_conversation(msg):\n",
    "    response = client.chat.completions.create(\n",
    "        model=deployment,\n",
    "        messages=msg,\n",
    "        functions=[\n",
    "            {\n",
    "                    \"name\": \"get_current_cluster_state\",\n",
    "                    \"description\": \"Get the current state in a given cluster\",\n",
    "                    \"parameters\": {\n",
    "                        \"type\": \"object\",\n",
    "                        \"properties\": {\n",
    "                            \"cluster_name\": {\n",
    "                                \"type\": \"string\",\n",
    "                                \"description\": \"the name of the cluster\",\n",
    "                            },\n",
    "\n",
    "                        },\n",
    "                        \"required\": [\"cluster_name\"],\n",
    "                    }\n",
    "              \n",
    "            }\n",
    "        ],\n",
    "        function_call=\"auto\",\n",
    "    )\n",
    "\n",
    "    response_message = response.choices[0].message\n",
    "    \n",
    "    function_call = response_message.function_call\n",
    "  \n",
    "    # 如果不需要调用function，则直接返回结果\n",
    "    if not function_call:\n",
    "        return response_message\n",
    "    function_name = function_call.name\n",
    "    function_to_call = funcs[function_name] \n",
    "    function_args = json.loads(response_message.function_call.arguments)\n",
    "    function_response = function_to_call(**function_args)                \n",
    "    msg.append( # adding assistant response to messages\n",
    "        {\n",
    "            \"role\": response_message.role,\n",
    "            \"function_call\": {\n",
    "                \"name\": function_name,\n",
    "                \"arguments\": response_message.function_call.arguments,\n",
    "            },\n",
    "            \"content\": None\n",
    "        }\n",
    "    )\n",
    "    msg.append( # adding function response to messages\n",
    "        {\n",
    "            \"role\": \"function\",\n",
    "            \"name\": function_name,\n",
    "            \"content\": function_response,\n",
    "        }\n",
    "    ) \n",
    "   \n",
    "    return run_conversation(msg)\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "b6856718",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cluster:DSP\n",
      "The issue with the cluster 'DSP' appears to be related to an image pulling problem. Specifically, it failed to pull the image `\"chaocai/docker/dsp:latest\"`.\n",
      "\n",
      "Here are a few suggestions to troubleshoot and potentially resolve the issue:\n",
      "\n",
      "1. **Check the Image Repository**: Ensure that the image repository `chaocai/docker/dsp` exists and that the `latest` tag is available within the repository.\n",
      "\n",
      "2. **Access Permissions**: Verify that you have the correct access permissions to the image repository. If it's a private repository, you'll need to ensure that the cluster has the necessary credentials to access it.\n",
      "\n",
      "3. **Network Connectivity**: Confirm that the network configuration is allowing the nodes in the DSP cluster to reach the image repository. This includes checking for any firewall rules, security groups, or other network policies that could prevent access.\n",
      "\n",
      "4. **Image Pull Policy**: Check the image pull policy for the pod to determine when the image should be pulled. If it's set to always pull, and the image is not accessible, you'll see this error. Consider adjusting the policy if needed.\n",
      "\n",
      "5. **Docker Hub Rate Limiting**: If the image repository is on Docker Hub, keep in mind that Docker Hub imposes rate limits on image pulls. If you've hit the limit, you may either wait for the limit to reset or upgrade your Docker Hub plan.\n",
      "\n",
      "6. **Registry Downtime or Issues**: The registry hosting the image may be experiencing downtime or other operational issues. Check the status of the registry to make sure it's operational.\n",
      "\n",
      "7. **Correctness of the Image Name**: Double-check the image name and tag to ensure they are spelled correctly and structured correctly in your deployment configuration.\n",
      "\n",
      "If none of these suggestions resolves the problem, you may need to dig deeper into the cluster logs to find more detailed error messages or consider reaching out to the support team for the image registry or your cloud provider.\n"
     ]
    }
   ],
   "source": [
    "#run(\"Hello\")\n",
    "print(run(\"What's wrong with the cluster 'DSP'? And if there's an error, give me some suggestion. \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b86826e9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
